﻿package net.wangit.framework.validation;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;

public class ValidationUtil {

	public ValidationUtil() {
	}

	public static final boolean isNotNull(ValidationResult vr,
			ActionContext ac, String value, ElementComponent ec) {
		if (Checker.isEmpty(value)) {
			String error = LabelUtil.get(ac, "system", "validation.notnull");
			vr.addError(0, ec.getId(), getMessage(ac, ec, value, error, false,
					0));
			return false;
		} else {
			return true;
		}
	}

	public static final String getMessage(ActionContext ac,
			ElementComponent ec, String value, String error, boolean multi,
			int i) {
		StringBuffer result = new StringBuffer(ElementUtil.resolveName(ac, ec));
		if (multi)
			result.append("[").append(i + 1).append("]");
		if (!Checker.isEmpty(value))
			result.append(" (").append(ec.getValue4Validation(value)).append(
					")");
		result.append(": ").append(error);
		return result.toString();
	}
}
